import requests
from bs4 import BeautifulSoup
import time

# 第一层url = ttps://cs.anjuke.com/community/p“1”/

# 静态爬取每一页中的"https://cs.anjuke.com/community/view/255088"


class GetxiangqingList:

    def __init__(self,url_with_p):
        self.url = url_with_p

    def get_html(self, url, headers, cookies):
        try:
            time.sleep(0.5)
            resp = requests.get(url=url, headers=headers , cookies=cookies)
            resp.encoding = 'utf-8'
            return resp.text
        except:
            pass

    def parse_data(self,html):
        soup = BeautifulSoup(html, 'html.parser')
        links = soup.find_all("a", class_="li-row")
        return links


    def save_data(self,links,Xurl_link):
        for link in links:
            href = link.get('href')
            Xurl_link.append(href)
            print(href)


    def run(self):
        headers = {
            'authority': 'cs.anjuke.com',
            'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
            'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
            'cache-control': 'no-cache',
            # 'cookie': 'SECKEY_ABVK=yN6sMd5ARVvSUB7X+vkXdWvn5eBpPhXdn13hkWGVZ/0%3D; BMAP_SECKEY=MXqX4OT-tHaH65jB1haMsbqW-qgBjmhRBGAY4WPuNkO3j4clIyRZde0H_7fzB-3tx98O28elBQiFIVd3E_-gHdB7123inHZzisq8NDx5Ps2ybuiHSrn56rQmrChwOabBO6WF8qlnruS9zHqTO2p5RvEZK9-bxK0ZitgaikWZ7PcAfmhYlPGHsUs4iG59AERB; aQQ_ajkguid=257EB14C-D5FC-4A0C-AE4C-B29EE0023134; sessid=ED113258-2FF4-4CD7-B0E7-E168D210D662; ajk-appVersion=; ctid=27; id58=CrIcp2WZmzZrrx8uRURFAg==; isp=true; xxzlcid=5ac0f643503149f1a01135e6b55d55c6; xxzl-cid=5ac0f643503149f1a01135e6b55d55c6; fzq_h=cd7ce9745e16db1bc94687924442373c_1704737951984_95d6e2b691194383a49fadab5eb18bdf_3740786969; twe=2; fzq_js_anjuke_ershoufang_pc=f39cf68ef60c18fcb3f2cf75b2d55029_1704781910567_25; obtain_by=2; ajk_member_verify=nGamCrFRDk0vJp%2FMiRoVQPDhLMdc%2FwFJFQTH9x1D0A8%3D; ajk_member_verify2=MjUxNzgyMzkxfEw3YlJ1Rkd8MQ%3D%3D; 58tj_uuid=86ff780a-1d02-4f6c-a3cd-f9f096505666; new_session=1; init_refer=; new_uv=1; als=0; _ga=GA1.2.576226070.1704782154; _gid=GA1.2.1221734514.1704782154; _ga_DYBJHZFBX2=GS1.2.1704782154.1.0.1704782154.0.0.0; ajk_member_id=251782391; fzq_js_anjuke_xiaoqu_pc=b58d4821fe3c7efa23924704a9d28218_1704782460670_23; ajkAuthTicket=TT=eadcc4caf4a62d9763ad5f35f6243bbc&TS=1704782457563&PBODY=R7I_W7aNyZJebCNApdksAHVy2IQRu1fg6jRH7V4zgu3IzENM4AVPdIJga1bYq80NjHYCGtYq9mL8d3xKrkV4eNQ2hYqoqrRHIVsOVGv7wk2mKN219NqwcT1Dwf4jVEQYNh_5sHCTONU1lQX8YCrI8SRRM607eMXxGQozUCLQ3k0&VER=2&CUID=39e7aRMHy6DDsOmgAVryN-JpTd-Z8guI; xxzl_cid=5ac0f643503149f1a01135e6b55d55c6; xxzl_deviceid=Ew780eRQKV4UCwJmW767KOiJMYWYbBcp41sFTHbQDhtbS2MuEz0hI6SK9c6STvg6',
            'pragma': 'no-cache',
            'referer': 'https://cs.anjuke.com/community/p1/',
            'sec-ch-ua': '"Microsoft Edge";v="119", "Chromium";v="119", "Not?A_Brand";v="24"',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-platform': '"Windows"',
            'sec-fetch-dest': 'document',
            'sec-fetch-mode': 'navigate',
            'sec-fetch-site': 'same-origin',
            'sec-fetch-user': '?1',
            'upgrade-insecure-requests': '1',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0',
        }
        cookies = {
            'SECKEY_ABVK': 'yN6sMd5ARVvSUB7X+vkXdWvn5eBpPhXdn13hkWGVZ/0%3D',
            'BMAP_SECKEY': 'MXqX4OT-tHaH65jB1haMsbqW-qgBjmhRBGAY4WPuNkO3j4clIyRZde0H_7fzB-3tx98O28elBQiFIVd3E_-gHdB7123inHZzisq8NDx5Ps2ybuiHSrn56rQmrChwOabBO6WF8qlnruS9zHqTO2p5RvEZK9-bxK0ZitgaikWZ7PcAfmhYlPGHsUs4iG59AERB',
            'aQQ_ajkguid': '257EB14C-D5FC-4A0C-AE4C-B29EE0023134',
            'sessid': 'ED113258-2FF4-4CD7-B0E7-E168D210D662',
            'ajk-appVersion': '',
            'ctid': '27',
            'id58': 'CrIcp2WZmzZrrx8uRURFAg==',
            'isp': 'true',
            'xxzlcid': '5ac0f643503149f1a01135e6b55d55c6',
            'xxzl-cid': '5ac0f643503149f1a01135e6b55d55c6',
            'fzq_h': 'cd7ce9745e16db1bc94687924442373c_1704737951984_95d6e2b691194383a49fadab5eb18bdf_3740786969',
            'twe': '2',
            'fzq_js_anjuke_ershoufang_pc': 'f39cf68ef60c18fcb3f2cf75b2d55029_1704781910567_25',
            'obtain_by': '2',
            'ajk_member_verify': 'nGamCrFRDk0vJp%2FMiRoVQPDhLMdc%2FwFJFQTH9x1D0A8%3D',
            'ajk_member_verify2': 'MjUxNzgyMzkxfEw3YlJ1Rkd8MQ%3D%3D',
            '58tj_uuid': '86ff780a-1d02-4f6c-a3cd-f9f096505666',
            'new_session': '1',
            'init_refer': '',
            'new_uv': '1',
            'als': '0',
            '_ga': 'GA1.2.576226070.1704782154',
            '_gid': 'GA1.2.1221734514.1704782154',
            '_ga_DYBJHZFBX2': 'GS1.2.1704782154.1.0.1704782154.0.0.0',
            'ajk_member_id': '251782391',
            'fzq_js_anjuke_xiaoqu_pc': 'b58d4821fe3c7efa23924704a9d28218_1704782460670_23',
            'ajkAuthTicket': 'TT=eadcc4caf4a62d9763ad5f35f6243bbc&TS=1704782457563&PBODY=R7I_W7aNyZJebCNApdksAHVy2IQRu1fg6jRH7V4zgu3IzENM4AVPdIJga1bYq80NjHYCGtYq9mL8d3xKrkV4eNQ2hYqoqrRHIVsOVGv7wk2mKN219NqwcT1Dwf4jVEQYNh_5sHCTONU1lQX8YCrI8SRRM607eMXxGQozUCLQ3k0&VER=2&CUID=39e7aRMHy6DDsOmgAVryN-JpTd-Z8guI',
            'xxzl_cid': '5ac0f643503149f1a01135e6b55d55c6',
            'xxzl_deviceid': 'Ew780eRQKV4UCwJmW767KOiJMYWYbBcp41sFTHbQDhtbS2MuEz0hI6SK9c6STvg6',
        }
        html = self.get_html(self.url, headers, cookies)
        links = self.parse_data(html)
        Xurl_link = []
        self.save_data(links, Xurl_link)
        return Xurl_link

def main1():
    data = []
    url = "https://cs.anjuke.com/community/"
    for i in range(1, 51):  # max=51
        url_with_p = f"{url}p{i}"
        crawler = GetxiangqingList(url_with_p)
        Xurl_link = crawler.run()
        data.extend(Xurl_link)
    return data


if __name__ == '__main__':
    main1()
